package test
{
	import away3d.containers.View3D;
	import away3d.entities.Mesh;
	import away3d.materials.ColorMaterial;
	import away3d.primitives.CubeGeometry;
	import away3d.primitives.PlaneGeometry;
	
	import awayphysics.collision.shapes.AWPBoxShape;
	import awayphysics.dynamics.AWPDynamicsWorld;
	import awayphysics.dynamics.AWPRigidBody;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Vector3D;
	import flash.utils.getTimer;

	[SWF(backgroundColor="#000000", frameRate="60", quality="LOW",width="800",height="600")]
	public class PhysicTest extends Sprite
	{
		private var view:View3D;
		private var physicWorld:AWPDynamicsWorld;
		private var time:int;
		public function PhysicTest()
		{
			initEngine();
			initObjects();
		}
		private function initEngine():void
		{
			view=new View3D();
			view.camera.y=300;
			view.camera.lookAt(new Vector3D());
			addChild(view);
			physicWorld=AWPDynamicsWorld.getInstance();
			physicWorld.initWithDbvtBroadphase();
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
			time=getTimer();
		}
		private function initObjects():void
		{
			var floor:Mesh=new Mesh(new CubeGeometry(1000,5,1000),new ColorMaterial(0xFF0000));
			view.scene.addChild(floor);
			var floorRightBody:AWPRigidBody=new AWPRigidBody(new AWPBoxShape(1000,5,5000),floor);
			physicWorld.addRigidBody(floorRightBody);
			var cube:Mesh=new Mesh(new CubeGeometry(),new ColorMaterial(0xFF00FF));
			view.scene.addChild(cube);
			var righbody:AWPRigidBody=new AWPRigidBody(new AWPBoxShape(),cube,1);
			righbody.position=new Vector3D(0,300,0);
			physicWorld.addRigidBody(righbody);
			//righbody.applyForce(new Vector3D(0,100,0),new Vector3D(0,10,0));
			//righbody.position=new Vector3D(0,400,0);
			//righbody.gravity=new Vector3D(0,-1,0);
			
			physicWorld.gravity=new Vector3D(0,-98,0);
		}
		private function onEnterFrame(event:Event):void
		{
			view.render();
			physicWorld.step(1.0 / 60);
		}
	}
}